home *** CD-ROM | disk | FTP | other *** search
- #include <Types.h>
- #include <memory.h>
- #include <Packages.h>
- #include <Errors.h>
- #include <quickdraw.h>
- #include <fonts.h>
- #include <dialogs.h>
- #include <windows.h>
- #include <menus.h>
- #include <events.h>
- #include <OSEvents.h>
- #include <Desk.h>
- #include <diskinit.h>
- #include <OSUtils.h>
- #include <resources.h>
- #include <toolutils.h>
- #include <AppleEvents.h>
- #include <EPPC.h>
- #include <GestaltEqu.h>
- #include <PPCToolbox.h>
- #include <Processes.h>
- #include <Aliases.h>
- #include <Files.h>
- #include <String.h>
- #include <Strings.h>
- #include <TextEdit.h>
- #include <StandardFile.h>
-
-
- #include "key.h"
-
- //#include "AEPackObject.h"
- //#include "AEObjects.h"
- //#include "AETypes.h"
-
- #define aeSelectionKeyword 'fsel'
- #define aeOpenSelection 'sope'
- #define kFinderSig 'FNDR'
- #define kSystemType 'MACS'
-
- OSErr FindAProcess(OSType typeToFind, OSType creatorToFind, ProcessSerialNumber* processSN);
-
- OSErr OpenSelection(FSSpec* theDocToOpen)
- {
- AppleEvent aeEvent; /* the event to create */
- AEDesc myAddressDesc, aeDirDesc, listElem; /* some descriptors I'll need */
- FSSpec dirSpec; /* FSSpec for the 'parent' directory of the file I'm opening */
- AEDesc fileList; /* my list */
- OSErr myErr; /* guess */
- ProcessSerialNumber process; /* This will hold the process serial number of the Finder */
- AliasHandle DirAlias, FileAlias; /* some aliases */
-
- /* go find the Finder's process information, please */
- if (FindAProcess(kFinderSig, kSystemType, &process))
- return(procNotFound); /* if I can't find the Finder, quit. Always check this, someone else */
-
- /* Create an address descriptor so the AppleEvent manager knows where to send this event */
- if (myErr = AECreateDesc(typeProcessSerialNumber, (Ptr)&process, sizeof(process), &myAddressDesc))
- return(myErr);
-
- /* Create the empty FinderEvent */
- /* it's a Finder 'FNDR', Open Selection 'sope' event */
- if (myErr = AECreateAppleEvent(kFinderSig, aeOpenSelection, &myAddressDesc, kAutoGenerateReturnID,
- kAnyTransactionID, &aeEvent))
- return(myErr);
-
- /* make a FSSpec for the parent folder (see the OpenSeletion description in the AE Registry ) */
- /* using the information in the document FSSpec */
- FSMakeFSSpec(theDocToOpen->vRefNum, theDocToOpen->parID, nil, &dirSpec);
- NewAlias(nil, &dirSpec, &DirAlias);
-
- /* Create alias for file */
- NewAlias(nil, theDocToOpen, &FileAlias);
- /* Create the file list */
- if (myErr = AECreateList(nil, 0, false, &fileList))
- return(myErr);
-
- /* create the folder descriptor */
- HLock((Handle)DirAlias);
- AECreateDesc(typeAlias, (Ptr)*DirAlias, GetHandleSize((Handle)DirAlias), &aeDirDesc);
- HUnlock((Handle)DirAlias);
- DisposHandle((Handle)DirAlias);
- /* put the Directory Desc in the event as the direct object */
- if ((myErr = AEPutParamDesc(&aeEvent, keyDirectObject, &aeDirDesc)) == noErr)
- {
- /* done with the desc, kill it */
- AEDisposeDesc(&aeDirDesc);
-
- /* create the file descriptor and add to aliasList */
- HLock((Handle)FileAlias);
- AECreateDesc(typeAlias, (Ptr)*FileAlias, GetHandleSize((Handle)FileAlias), &listElem);
- HLock((Handle)FileAlias);
- DisposHandle((Handle)FileAlias);
- myErr = AEPutDesc(&fileList, 0, &listElem);
- }
-
- if (myErr)
- return(myErr);
- AEDisposeDesc(&listElem);
- /* Add the file alias list to the event */
- if (myErr = AEPutParamDesc(&aeEvent, aeSelectionKeyword, &fileList))
- return(myErr);
- myErr = AEDisposeDesc(&fileList);
-
- /* And now send the event! */
- myErr = AESend(&aeEvent, nil, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil, nil);
-
- /* and kill the memory used */
- AEDisposeDesc(&aeEvent);
- }
-
-
- /* This runs through the process list looking for the indicated application */
- OSErr FindAProcess(OSType typeToFind, OSType creatorToFind, ProcessSerialNumber* processSN)
- {
- ProcessInfoRec tempInfo;
- FSSpec procSpec;
- Str31 processName;
- OSErr myErr = noErr;
-
- /* nul out the PSN so we're starting at the beginning of the list */
- processSN->lowLongOfPSN = kNoProcess;
- processSN->highLongOfPSN = kNoProcess;
-
- /* initialize the process information record */
- tempInfo.processInfoLength = sizeof(ProcessInfoRec);
- tempInfo.processName = processName;
- tempInfo.processAppSpec = &procSpec;
-
- /* loop through all the processes until we */
- /* 1) find the process we want */
- /* 2) error out because of some reason (usually, no more processes */
-
- myErr = GetNextProcess(processSN);
- while (myErr == noErr)
- {
- GetProcessInformation(processSN, &tempInfo);
- p2cstr(processName);
- keyPrint("Process=%s\n", processName);
-
- if (tempInfo.processSignature == creatorToFind && tempInfo.processType == typeToFind)
- break;
-
- keyPrint("Didnt like it\n");
-
- myErr = GetNextProcess(processSN);
- }
-
- return myErr;
- }
-
-
- OSErr SendOpenDocument(ProcessSerialNumber* tgtAddress, FSSpec* fileSpec)
- {
- SFTypeList fileType_list= {'TEXT','PDF '}; /* PDF 1.0 = 'TEXT', PDF 1.1 = 'PDF ' */
- AliasHandle file_alias;
- AEDesc fileDesc;
- AEDescList the_list;
- AppleEvent appleEvent, replyEvent;
- OSErr err = noErr;
- Boolean isInvisible=false;
- AEDesc myAddressDesc;
-
- /* Init stuff */
- the_list.dataHandle = NULL;
- appleEvent.dataHandle = NULL;
-
- /* Create an address descriptor so the AppleEvent manager knows where to send this event */
- err = AECreateDesc(typeProcessSerialNumber, (Ptr)tgtAddress, sizeof(ProcessSerialNumber), &myAddressDesc);
- if (err)
- return err;
-
- /* Create the Apple Event */
- if (err == noErr)
- err = AECreateAppleEvent (kCoreEventClass,
- kAEOpenDocuments,
- &myAddressDesc,
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &appleEvent );
-
- /* Create an empty list for the descriptors */
- if (err == noErr)
- err = AECreateList (NULL, 0, false, &the_list);
-
- /* Create an alias to the file */
- if (err == noErr) {
- err = NewAliasMinimal (fileSpec, &file_alias);
- }
-
- /* Create a descriptor with the alias and put it into the list */
- if (err == noErr) {
- fileDesc.descriptorType = typeAlias;
- fileDesc.dataHandle = (Handle) file_alias;
- err = AEPutDesc (&the_list, 0, &fileDesc);
- }
-
- /* Put the list into the apple event */
- if (err == noErr)
- err = AEPutParamDesc( &appleEvent, keyDirectObject, &the_list );
-
- /* add the invisible attribute */
- err = AEPutParamPtr(&appleEvent, 'nvis', typeBoolean,
- (Ptr)&isInvisible, sizeof(isInvisible));
-
- /* Send the event */
- if (err == noErr) {
- err = AESend (&appleEvent,
- &replyEvent,
- kAEWaitReply+kAECanInteract+kAECanSwitchLayer,
- kAENormalPriority,
- 600,
- NULL,
- NULL);
- }
-
- AEDisposeDesc(&appleEvent);
- AEDisposeDesc(&replyEvent);
-
- return err;
- }
-
- static OSErr LaunchApp(char* theApp, ProcessSerialNumber* theSN)
- {
- Str255 path;
- OSErr err;
- FSSpec spec;
- LaunchParamBlockRec launchPB;
-
- strncpy((char *)path, (char *)theApp, 254);
- path[254] = '\0';
- c2pstr((char *)path);
-
- err = FSMakeFSSpec(0, 0, path, &spec);
- if (err != noErr)
- return err;
-
- launchPB.launchBlockID = extendedBlock;
- launchPB.launchEPBLength = extendedBlockLen;
- launchPB.launchFileFlags = 0;
- launchPB.launchControlFlags = launchContinue + launchDontSwitch + launchNoFileFlags;
- launchPB.launchAppSpec = &spec;
- launchPB.launchAppParameters = nil;
-
- err = LaunchApplication(&launchPB);
- if (err != noErr)
- return err;
-
- *theSN = launchPB.launchProcessSN;
-
- return err;
- }
-
- #ifdef OUT
- int GotoPage(pageNum)
- {
- Str255 path;
- OSErr err;
- FSSpec spec;
- AppleEvent appleEvent;
- ProcessSerialNumber psn;
- AEDesc myAddressDesc;
- // long pageNum;
-
- SysBeep(2);
-
- if (FindAProcess('APPL', 'CARO', &psn))
- {
- keyPrint("Cant find acrobat application\n");
- return -1;
- }
-
- /* Create an address descriptor so the AppleEvent manager knows where to send this event */
- err = AECreateDesc(typeProcessSerialNumber, (Ptr)&psn, sizeof(psn), &myAddressDesc);
- if (err)
- {
- keyPrint("Error in AECreateDesc = %d\n", err);
- return err;
- }
-
- err = AECreateAppleEvent(kAEAcrobatViewerSuite,
- kAEGotoPage,
- &myAddressDesc,
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &appleEvent);
- if (err)
- {
- keyPrint("Error in AECreateDesc = %d\n", err);
- return err;
- }
-
- err = AEPutParamPtr(&appleEvent, keyAEPageNumber, typeLongInteger, &pageNum, sizeof(pageNum));
- if (err)
- {
- keyPrint("Error in AEPutParamPtr = %d\n", err);
- return err;
- }
-
- /* And now send the event! */
- err = AESend(&appleEvent, nil, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer, kAENormalPriority,
- kAEDefaultTimeout, nil, nil);
-
- /* and kill the memory used */
- AEDisposeDesc(&appleEvent);
-
- if (err)
- keyPrint("Error in AESend = %d", err);
-
- return err;
- }
- #endif
-
- void MyLauncherGotoPage(key* the)
- {
- char theApp[256];
- char theDoc[256];
- int thePageNum;
- EventRecord dummyEvent;
- OSErr err;
- FSSpec spec;
- ProcessSerialNumber psn;
- long count;
-
- thePageNum = keyToInteger(the[ARGUMENT(1)]);
-
- the[RESULT] = keyFalse;
-
- // GotoPage(thePageNum);
-
- return;
- }
-
- void MyLauncherOpen(key* the)
- {
- char theApp[256];
- char theDoc[256];
- int thePageNum;
- EventRecord dummyEvent;
- OSErr err;
- FSSpec spec;
- ProcessSerialNumber psn;
- long count;
-
- // strncpy(theApp, keyToString(the[ARGUMENT(1)]), 254);
- // theApp[254] = '\0';
-
- strncpy(theDoc, keyToString(the[ARGUMENT(1)]), 254);
- theDoc[254] = '\0';
- c2pstr(theDoc);
-
- // thePageNum = keyToInteger(the[ARGUMENT(3)]);
-
- the[RESULT] = keyFalse;
-
- err = FSMakeFSSpec(0, 0, (unsigned char const *)theDoc, &spec);
- if (err != noErr)
- {
- keyPrint("Error in FSMakeFSSpec = %d", err);
- return;
- }
-
- err = OpenSelection(&spec);
- if (err != noErr)
- {
- keyPrint("Error in OpenSelection = %d", err);
- return;
- }
-
- the[RESULT] = keyTrue;
-
- return;
-
- err = LaunchApp(theApp, &psn);
- if (err != noErr)
- {
- keyPrint("Error in LaunchApp = %d", err);
- return;
- }
-
- err = SendOpenDocument(&psn, &spec);
- if (err != noErr)
- {
- keyPrint("Error in LaunchApp = %d", err);
- return;
- }
-
- // now wait a bit, and then bring the app to the front
- WaitNextEvent(0, &dummyEvent, 180, nil); // check every second
-
- SetFrontProcess(&psn);
-
- the[RESULT] = keyTrue;
- }
-
-
-
-
- void MyChapterOpen(key* the)
- {
- char theApp[256];
- char theDoc[256];
- int thePageNum;
- EventRecord dummyEvent;
- OSErr err;
- FSSpec spec;
- ProcessSerialNumber psn;
- long count;
-
- // strncpy(theApp, keyToString(the[ARGUMENT(1)]), 254);
- // theApp[254] = '\0';
-
- strncpy(theDoc, keyToString(the[ARGUMENT(1)]), 254);
- theDoc[254] = '\0';
- c2pstr(theDoc);
-
- // thePageNum = keyToInteger(the[ARGUMENT(3)]);
-
- the[RESULT] = keyFalse;
-
- err = FSMakeFSSpec(0, 0, (unsigned char const *)theDoc, &spec);
- if (err != noErr)
- {
- keyPrint("Error in FSMakeFSSpec = %d", err);
- return;
- }
-
- err = OpenSelection(&spec);
- if (err != noErr)
- {
- keyPrint("Error in OpenSelection = %d", err);
- return;
- }
-
- the[RESULT] = keyTrue;
-
- return;
-
- err = LaunchApp(theApp, &psn);
- if (err != noErr)
- {
- keyPrint("Error in LaunchApp = %d", err);
- return;
- }
-
- err = SendOpenDocument(&psn, &spec);
- if (err != noErr)
- {
- keyPrint("Error in LaunchApp = %d", err);
- return;
- }
-
- // now wait a bit, and then bring the app to the front
- WaitNextEvent(0, &dummyEvent, 180, nil); // check every second
-
- SetFrontProcess(&psn);
-
- the[RESULT] = keyTrue;
- }
-
-
-
- void MyAcroLauncherOpen(key* the)
- {
- char theApp[256];
- char theDoc[256];
- int thePageNum;
- EventRecord dummyEvent;
- OSErr err;
- FSSpec spec;
- ProcessSerialNumber psn;
- long count;
-
- // strncpy(theApp, keyToString(the[ARGUMENT(1)]), 254);
- // theApp[254] = '\0';
-
- strncpy(theDoc, keyToString(the[ARGUMENT(1)]), 254);
- theDoc[254] = '\0';
- c2pstr(theDoc);
-
- // thePageNum = keyToInteger(the[ARGUMENT(3)]);
-
- the[RESULT] = keyFalse;
-
- err = FSMakeFSSpec(0, 0, (unsigned char const *)theDoc, &spec);
- if (err != noErr)
- {
- keyPrint("Error in FSMakeFSSpec = %d", err);
- return;
- }
-
- err = OpenSelection(&spec);
- if (err != noErr)
- {
- keyPrint("Error in OpenSelection = %d", err);
- return;
- }
-
- the[RESULT] = keyTrue;
-
- return;
-
- err = LaunchApp(theApp, &psn);
- if (err != noErr)
- {
- keyPrint("Error in LaunchApp = %d", err);
- return;
- }
-
- err = SendOpenDocument(&psn, &spec);
- if (err != noErr)
- {
- keyPrint("Error in LaunchApp = %d", err);
- return;
- }
-
- // now wait a bit, and then bring the app to the front
- WaitNextEvent(0, &dummyEvent, 180, nil); // check every second
-
- SetFrontProcess(&psn);
-
- the[RESULT] = keyTrue;
- }
-
-
-